package cn.edu.nwafu.core.controller.api;


import cn.edu.nwafu.common.result.R;
import cn.edu.nwafu.core.pojo.entity.AircraftInfo;
import cn.edu.nwafu.core.service.AircraftInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhanghao
 * @since 2021-10-30
 */
@RestController
@Slf4j
@Api(tags = "航班管理")
@RequestMapping("/api/core/aircraftInfo")
public class AircraftInfoController {

    @Autowired
    private AircraftInfoService aircraftInfoService;


    @ApiOperation("查询")
    @PostMapping("search")
    public R search(
            @ApiParam("查询航班信息")
            @RequestBody AircraftInfo aircraftInfo
    ) {

        String startCity = aircraftInfo.getStartCity();
        String reachCity = aircraftInfo.getReachCity();
        Date startTime = aircraftInfo.getStartTime();
        Date reachTime = aircraftInfo.getReachTime();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<AircraftInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(startCity), "start_city", startCity);
        queryWrapper.eq(StringUtils.isNotBlank(reachCity), "reach_city", reachCity);
        if (startTime != null) {
            String start = simpleDateFormat.format(startTime);
            queryWrapper.eq("start_time", start);
        }


        List<AircraftInfo> list = aircraftInfoService.list(queryWrapper);

        queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(StringUtils.isNotBlank(startCity), "start_city", reachCity)
                .eq(StringUtils.isNotBlank(reachCity), "reach_city", startCity);

        if (reachTime != null) {
            String reach = simpleDateFormat.format(reachTime);
            queryWrapper.eq("start_time", reach);
        }


        List<AircraftInfo> list1 = aircraftInfoService.list(queryWrapper);

        if (StringUtils.isNotBlank(startCity) && StringUtils.isNotBlank(reachCity)) {
            list.addAll(list1);
        }

        return R.ok().data("aircraftInfo", list);
    }

    @ApiOperation("根据id查询航班")
    @GetMapping("/get/{id}")
    public R get(
            @ApiParam("所查询航班id")
            @PathVariable Long id
    ) {
        AircraftInfo aircraftInfo = aircraftInfoService.getById(id);

        return R.ok().data("aircraftInfo", aircraftInfo);
    }
}

